var SettingHandler = function () {
}
SettingHandler.prototype.listenAvatarUploadEvent = function () {
    $("#upload_avatar").on("change", function (event) {
        var image = this.files[0];
        var formdata = new FormData();
        formdata.append("image", image);
        myajax.post({
            url: "/avatar/upload/",
            data: formdata,
            // 如果使用jQuery上传文件，那么还需要指定以下两个参数
            processData: false,
            contentType: false,
            success: function (result) {
                if (result['code'] == 200) {
                    var avatar = result['data']['avatar'];
                    var url = "/media/avatar/" + avatar;
                    $("#avatar-img").attr("src", url)
                }
            }
        });
    });
}
SettingHandler.prototype.listenProfileSaveEvent = function () {
    $("#btn_save").on("click", function (event) {
        event.preventDefault();
        var signature = $("input[name='signature']").val();
        if (signature && (signature.length > 50 || signature.length < 2)) {
            alert("签名长度必须在2-50字之间！");
            return;
        }
        myajax.post({
            url: "/profile/edit/",
            data: {"signature": signature},
            success: function (result) {
                if (result['code'] == 200) {
                    alert('保存成功')
                } else {
                    alert(result['message'])
                }
            }

        })
    });
}
SettingHandler.prototype.run = function () {
    this.listenAvatarUploadEvent();
    this.listenProfileSaveEvent();
}
$(function () {
    var handler = new SettingHandler();
    handler.run();
});